package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.ActivityDTO;
import com.ktjy.pojo.TbActivity;
import com.ktjy.service.ITbActivityService;
import com.ktjy.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 活动管理 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-08-02
 */
@RestController
@RequestMapping("/api/tb/activity")
@Tag(name="活动模块")
public class TbActivityController {
    Logger log= LoggerFactory.getLogger(TbActivityController.class);

    @Autowired
    ITbActivityService activityService;

    @GetMapping("/list")
    @Operation(summary="活动列表接口")
    public AjaxResult list(ActivityDTO activityDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        IPage<TbActivity> page = activityService.findPage(activityDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @PostMapping("/save")
    @Operation(summary="活动保存接口")
    public AjaxResult saveOrUpdate(@RequestBody TbActivity tbActivity){
        try {
            boolean ok=activityService.saveOrUpdate(tbActivity);
            if(ok){
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("TbActivityController中saveOrUpdate方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @GetMapping("/view/{id}")
    @Operation(summary="活动详情接口")
    public AjaxResult view(@PathVariable("id") Long id){
        //查询详情
        try {
            TbActivity tbActivity=activityService.getById(id);
            if(tbActivity!=null){
                return AjaxResult.success(tbActivity);
            }
        } catch (Exception e) {
            log.error("TbActivityController中view方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/del/{id}")
    @Operation(summary="活动删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            boolean ok= activityService.removeById(id);
            if(ok){//删除成功
                return AjaxResult.success("删除成功");
            }
        } catch (Exception e) {
            log.error("TbActivityController中del方法执行报错的。",e);
        }
        return AjaxResult.error("删除失败");
    }

    @PutMapping("/pass")
    @Operation(summary="活动通过接口")
    public AjaxResult pass(@RequestBody TbActivity tbActivity){
        try {
            boolean ok=activityService.updateById(tbActivity);
            if(ok){
                return AjaxResult.success();
            }
        } catch (Exception e) {
            log.error("TbActivityController中pass方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @PutMapping("/reject")
    @Operation(summary="活动拒绝接口")
    public AjaxResult reject(@RequestBody TbActivity tbActivity){
        try {
            boolean ok=activityService.updateById(tbActivity);
            if(ok){
                return AjaxResult.success();
            }
        } catch (Exception e) {
            log.error("TbActivityController中reject方法执行报错的。",e);
        }
        return AjaxResult.error();
    }
}
